//
//  HZGoodsCommentCell.m
//  app
//
//  Created by 刘华舟 on 15/5/26.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZGoodsCommentCell.h"

#import "UIButton+URL.h"
#import "NSString+Conveniently.h"
#import "NSDate+Conveniently.h"
#import "HZTableView.h"
#import "HZGoodscommentModel.h"

static const NSUInteger kCellHeight = kHZGoodsCommentCellHeight;
static const NSInteger kPictureButtonHeight = kHZGoodsCommentPictureHeight;
static const NSInteger kStarImageViewHeight = 30;
static const NSInteger kNicknameLabelWidth = 240;
static const NSInteger kTimeLabelWidth = 120;
static const NSInteger kTopLabelHeight = 30;

@interface HZGoodsCommentCell()

@property (strong, nonatomic) NSMutableArray *fiveStars;
@property (strong, nonatomic) UILabel *nicknameLabel;
@property (strong, nonatomic) UILabel *timeLabel;
@property (strong, nonatomic) UILabel *commentsLabel;
@property (strong, nonatomic) UIImageView *replyView;
@property (strong, nonatomic) UILabel *replyLabel;

@property (strong, nonatomic) UIScrollView *scrollView;

@property (strong, nonatomic) NSMutableArray* picBtns;
@property (strong, nonatomic) NSArray* picDatas;


@end

@implementation HZGoodsCommentCell

- (NSMutableArray *)fiveStars{
    if (_fiveStars == nil) {
        _fiveStars = [NSMutableArray array];
    }
    return _fiveStars;
}

- (NSMutableArray *)picBtns{
    if (_picBtns == nil) {
        _picBtns = [NSMutableArray array];
    }
    return _picBtns;
}

-(void)drawRect:(CGRect)rect
{
    [self drawBackground:self.bounds color:[UIColor whiteColor]];
    [self drawHorizonLineLeft:[self.ubo uw:0] right:[self.ubo uw:0] at:[self.ubo fy:UB_BOTTOM py:-1]];
}

+ (instancetype)cellWithTableView:(HZTableView *)tableView{
    NSString *cellReuseIdentifier = @"HZGoodsCommentCell";
    
    HZGoodsCommentCell * cell= (HZGoodsCommentCell *)[tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
    if (cell == nil) {
        cell = [[HZGoodsCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.frame = CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP], [UBO_SCREEN fw:UB_WIDTH uh:88]);
        
        self.contentMode = UIViewContentModeRedraw;
        
        self.backgroundColor = HZ_WHITE_COLOR;
        
        //星星图标
        for (NSInteger i = 0; i<5 ; i++) {
            UIImageView *imgView = nil;
            if (i == 0) {
                imgView = [[UIImageView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT ux:kSizeMallViewMargin fy:UB_TOP uy:kSizeMallViewMargin],
                                                                      [self.ubo uw:kStarImageViewHeight uh:kStarImageViewHeight])];
            }else{
                UIImageView *lastStar = [self.fiveStars lastObject];
                imgView = [[UIImageView alloc] initWithFrame:CGRectOf([lastStar.ufo fx:UB_RIGHT fy:UB_TOP],
                                                                      [lastStar.ubo fw:UB_WIDTH fh:UB_HEIGHT])];
            }
            
            [imgView setImage:[UIImage imageNamed:kImageMallGoodsDetailLikeStarNormal]];
            imgView.contentMode = UIViewContentModeCenter;
            [self.fiveStars addObject:imgView];
            [self addSubview:imgView];
        }
        
        //时间
        
        self.timeLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.ubo fx:UB_RIGHT ux:-kSizeMallViewMargin-kTimeLabelWidth fy:UB_TOP uy:kSizeMallViewMargin],
                                                                    [self.ubo uw:kTimeLabelWidth uh:kTopLabelHeight])];
        
        
        self.timeLabel.textAlignment = NSTextAlignmentRight;
        self.timeLabel.textColor = HZ_MALL_TEXT_M_LIGHTGRAY_COLOR;
        self.timeLabel.font = JKAppGlobleFontMinTagText;
        [self addSubview:self.timeLabel];
        
        //昵称
        self.nicknameLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.timeLabel.ufo fx:UB_LEFT ux:-kNicknameLabelWidth-kSizeMallViewMargin fy:UB_TOP],
                                                                 [self.ubo uw:kNicknameLabelWidth uh:kTopLabelHeight])];
        
        
        self.nicknameLabel.textAlignment = NSTextAlignmentRight;
        self.nicknameLabel.textColor = HZ_MALL_TEXT_M_LIGHTGRAY_COLOR;
        self.nicknameLabel.font = JKAppGlobleFontMinTagText;
        [self addSubview:self.nicknameLabel];
        
        //评论
        self.commentsLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT ux:kSizeMallViewMargin fy:UB_TOP uy:kTopLabelHeight+kSizeMallViewMargin],
                                                                     [self.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin uh:0])];
        
        
        self.commentsLabel.textAlignment = NSTextAlignmentLeft;
        self.commentsLabel.textColor = HZ_MALL_TEXT_M_LIGHTGRAY_COLOR;
        self.commentsLabel.font = JKAppGlobleFontSmallTagText;
        self.commentsLabel.numberOfLines = 0;
        [self addSubview:self.commentsLabel];
        
        //scrollView
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectOf([self.commentsLabel.ufo fx:UB_LEFT fy:UB_BOTTOM uy:kSizeMallViewMargin],
                                                                       [self.commentsLabel.ubo fw:UB_WIDTH uh:0])];
        self.scrollView.backgroundColor = HZ_WHITE_COLOR;
        self.scrollView.showsHorizontalScrollIndicator = YES;
        [self addSubview:self.scrollView];
        
        //客服回复
        self.replyView = [[UIImageView alloc] initWithFrame:CGRectOf([self.scrollView.ufo fx:UB_LEFT fy:UB_BOTTOM uy:kSizeMallViewMargin],
                                                                [self.scrollView.ubo fw:UB_WIDTH uh:0])];
        self.replyView.backgroundColor = HZ_CLEAR_COLOR;
        UIImage *replyBackImage =  [[UIImage imageNamed:kImageResponeBackgournd] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 27, 16, 28) resizingMode:UIImageResizingModeStretch];
        [self.replyView setImage:replyBackImage];
        
        
        self.replyLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.replyView.ubo fx:UB_LEFT ux:kSizeMallViewMargin fy:UB_TOP uy:kSizeMallViewMargin],
                                                                     [self.replyView.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin uh:0])];
        
        
        self.replyLabel.textAlignment = NSTextAlignmentLeft;
        self.replyLabel.textColor = HZ_MALL_TEXT_M_LIGHTGRAY_COLOR;
        self.replyLabel.font = JKAppGlobleFontContentText;
        self.replyLabel.numberOfLines = 0;
        [self.replyView addSubview:self.replyLabel];
        
        [self addSubview:self.replyView];
    }
    return self;
}

-(void)showCategories:(NSArray*)categories imageKey:(NSString *)imageKey;
{
    
    
    for(UIButton* cell in self.picBtns){
        [cell removeFromSuperview];
    }
    
    [self.picBtns removeAllObjects];
    self.picDatas = categories;
    if ([categories isEmpty]) {
        return;
    }
    
    NSInteger index = 0;
    
    for(NSDictionary *cate in categories){
        UIButton *btn = nil;
        
        if (index == 0) {
            btn = [[UIButton alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP], [self.ubo uw:kPictureButtonHeight uh:kPictureButtonHeight])];
        }else{
            UIButton *lastBtn = [self.picBtns lastObject];
            btn = [[UIButton alloc] initWithFrame:CGRectOf([lastBtn.ufo fx:UB_LEFT ux:kSizeMallViewMargin fy:UB_BOTTOM], [lastBtn.ubo fw:UB_WIDTH fh:UB_HEIGHT])];
        }
        
        [btn setBackgroundColor:HZ_MALL_M_LIGHT_GRAY_BACKGROUND_COLOR];
        
        [btn setImageForState:UIControlStateNormal withString:[cate stringForKey:imageKey nilValue:@""]];
        [btn addTarget:self action:@selector(labelDidClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.scrollView addSubview:btn];
        [self.picBtns addObject:btn];
        
        index++;
        
    }
    
    float scrollContentWidth = [self.ubo uw:kPictureButtonHeight+kSizeMallViewMargin] * index;
    
    if (scrollContentWidth > [self.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin]) {
        scrollContentWidth = [self.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin];
    }
    
    self.scrollView.contentSize = [self.scrollView.ubo ow:scrollContentWidth fh:UB_HEIGHT];

}


-(void)labelDidClicked:(UIButton*)btn
{
    CGFloat btnY = btn.ufo.y;
    CGFloat btnMaxY = self.scrollView.contentSize.height - self.scrollView.frame.size.height;
    
    if (btnY <= btnMaxY) {
        [self.scrollView setContentOffset:CGPointMake(0.0f, btnY) animated:YES];
    }else{
        [self.scrollView setContentOffset:CGPointMake(0.0f, btnMaxY) animated:YES];
    }
    

    NSDictionary *dict = @{@"array":self.picDatas,
                           @"index":[NSNumber numberWithInteger:btn.tag],
                           @"urlKey":APIKEY_mall_url};

    
    if (self.picsTargetCaller) {
        [self.picsTargetCaller call:dict];
    }
}

- (void)setModel:(HZGoodsCommentModel *)model{
    _model = model;
    
    //计算星星
    NSInteger starNum = ([model.scores integerValue]+1)/2;
    NSInteger index = 0;
    for (UIImageView *imgV in self.fiveStars) {
        if (starNum>=index) {
            [imgV setImage:[UIImage imageNamed:kImageMallGoodsDetailLikeStarSelected]];
        }else{
            [imgV setImage:[UIImage imageNamed:kImageMallGoodsDetailLikeStarNormal]];
        }
    }
    
    //昵称
    self.nicknameLabel.text = model.nickname;
    //时间
    self.timeLabel.text = [[[NSDate alloc] initWithTimeIntervalSince1970:[model.dateline floatValue]] stringWithFormat:@"yyyy-MM-dd"];
    
    //评论
    if (model.contents.length) {
        CGSize contentTextSize = [model.contents boundingRectWithSize:[UBO_SCREEN fw:UB_WIDTH uw:-(kSizeMallViewMargin*2) uh:kContentTextMaxHeight] options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:JKAppGlobleFontSmallTagText} context:nil].size;
        
        self.commentsLabel.frame = CGRectOf([self.ubo fx:UB_LEFT ux:kSizeMallViewMargin fy:UB_TOP uy:kTopLabelHeight+2*kSizeMallViewMargin],
                                            [self.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin oh:contentTextSize.height]);
        self.commentsLabel.text = model.contents;
    }else{
        self.commentsLabel.frame = CGRectOf([self.ubo fx:UB_LEFT ux:kSizeMallViewMargin fy:UB_TOP uy:kTopLabelHeight+kSizeMallViewMargin],
                                            [self.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin oh:0]);
        self.commentsLabel.text = @"";
    }
    //图片
    if (model.pics.count) {
        self.scrollView.frame = CGRectOf([self.commentsLabel.ufo fx:UB_LEFT fy:UB_BOTTOM uy:kSizeMallViewMargin],
                                         [self.commentsLabel.ubo fw:UB_WIDTH uh:kPictureButtonHeight]);
        
        [self showCategories:model.pics imageKey:APIKEY_mall_url];
    }else{
        self.scrollView.frame = CGRectOf([self.commentsLabel.ufo fx:UB_LEFT fy:UB_BOTTOM],
                                         [self.commentsLabel.ubo fw:UB_WIDTH uh:0]);
        
        [self showCategories:model.pics imageKey:APIKEY_mall_url];
    }
    //回复
    if (model.replys.count) {
        NSString *replys = [model.replys stringForKey:APIKEY_mall_contents nilValue:@""];
        CGSize contentTextSize = [[NSString stringWithFormat:@"客服回复：%@", replys] boundingRectWithSize:[UBO_SCREEN fw:UB_WIDTH uw:-(kSizeMallViewMargin*4) uh:kContentTextMaxHeight] options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:JKAppGlobleFontSmallTagText} context:nil].size;
        self.replyView.frame = CGRectOf([self.scrollView.ufo fx:UB_LEFT fy:UB_BOTTOM uy:kSizeMallViewMargin],
                                        [self.scrollView.ubo fw:UB_WIDTH uh:2*kSizeMallViewMargin oh:contentTextSize.height]);
        self.replyLabel.frame = CGRectOf([self.replyView.ubo fx:UB_LEFT ux:kSizeMallViewMargin fy:UB_TOP uy:kSizeMallViewMargin],
                                         [self.replyView.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin oh:contentTextSize.height]);
        NSMutableAttributedString *atbStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"客服回复：%@", replys] attributes:@{NSFontAttributeName:JKAppGlobleFontSmallTagText}];
        [atbStr addAttributes:@{NSForegroundColorAttributeName:HZ_MALL_MAIN_THEME_ORANGE} range:NSMakeRange(0, 5)];
        [atbStr addAttributes:@{NSForegroundColorAttributeName:HZ_MALL_TEXT_M_LIGHTGRAY_COLOR} range:NSMakeRange(5, replys.length)];
        self.replyLabel.attributedText = atbStr;
    }else{
        self.replyView.frame = CGRectOf([self.scrollView.ufo fx:UB_LEFT fy:UB_BOTTOM],
                                        [self.scrollView.ubo fw:UB_WIDTH uh:0]);
        self.replyLabel.frame = CGRectOf([self.replyView.ubo fx:UB_LEFT ux:kSizeMallViewMargin fy:UB_TOP],
                                         [self.replyView.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin oh:0]);
         NSMutableAttributedString *atbStr = [[NSMutableAttributedString alloc] initWithString:@"" attributes:@{NSFontAttributeName:JKAppGlobleFontSmallTagText}];
        self.replyLabel.attributedText = atbStr;
    }
    
}


@end
